package com.jett.JFtpServer.Filter;

import cn.hutool.log.Log;
import org.apache.ftpserver.ipfilter.SessionFilter;
import org.apache.mina.core.session.IoSession;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class JFtpServerWriteLIstFilter  implements SessionFilter {
    private static final Log log = Log.get();
    private static final String SPLIT_REGEX = ";";
    private String ipWriteListStr = null;

    public JFtpServerWriteLIstFilter() {
    }

    public JFtpServerWriteLIstFilter(String ipWriteList) {
        this.ipWriteListStr = ipWriteList;
    }

    @Override
    public boolean accept(IoSession session) {
        InetAddress address = ((InetSocketAddress)session.getRemoteAddress()).getAddress();
        if (this.ipWriteListStr != null && this.ipWriteListStr.trim().length() != 0) {
            String[] ipWriteList = this.ipWriteListStr.split(";");
            int var5 = ipWriteList.length;
            byte var6 = 0;
            if (var6 < var5) {
                String ipStr = ipWriteList[var6];
                if (ipStr.hashCode() == address.getHostAddress().hashCode()) {
                    log.info("客户端IP:" + address.getHostAddress() + "在白名单列表内,允许登录!");
                    return true;
                } else {
                    log.error("客户端IP:" + address.getHostAddress() + "不在白名单列表内,不允许登录!");
                    return false;
                }
            } else {
                return false;
            }
        } else {
            log.error("IP白名单列表为空,不进行IP白名单校验!");
            return true;
        }
    }

}
